﻿using Microsoft.EntityFrameworkCore;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.Identity;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.IdentityServer.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.TenantManagement;
using Volo.Abp.TenantManagement.EntityFrameworkCore;
using WisDom.SmartTMS.Entities;

namespace WisDom.SmartTMS.EntityFrameworkCore
{
    [ReplaceDbContext(typeof(IIdentityDbContext))]
    [ReplaceDbContext(typeof(ITenantManagementDbContext))]
    [ConnectionStringName("Default")]
    public class SmartTMSDbContext : 
        AbpDbContext<SmartTMSDbContext>,
        IIdentityDbContext,
        ITenantManagementDbContext
    {
        /* Add DbSet properties for your Aggregate Roots / Entities here. */
        
        #region Entities from the modules
        
        /* Notice: We only implemented IIdentityDbContext and ITenantManagementDbContext
         * and replaced them for this DbContext. This allows you to perform JOIN
         * queries for the entities of these modules over the repositories easily. You
         * typically don't need that for other modules. But, if you need, you can
         * implement the DbContext interface of the needed module and use ReplaceDbContext
         * attribute just like IIdentityDbContext and ITenantManagementDbContext.
         *
         * More info: Replacing a DbContext of a module ensures that the related module
         * uses this DbContext on runtime. Otherwise, it will use its own DbContext class.
         */
        
        //Identity
        public DbSet<IdentityUser> Users { get; set; }
        public DbSet<IdentityRole> Roles { get; set; }
        public DbSet<IdentityClaimType> ClaimTypes { get; set; }
        public DbSet<OrganizationUnit> OrganizationUnits { get; set; }
        public DbSet<IdentitySecurityLog> SecurityLogs { get; set; }
        public DbSet<IdentityLinkUser> LinkUsers { get; set; }
        
        // Tenant Management
        public DbSet<Tenant> Tenants { get; set; }
        public DbSet<TenantConnectionString> TenantConnectionStrings { get; set; }

        #endregion
        public SmartTMSDbContext(DbContextOptions<SmartTMSDbContext> options)
            : base(options)
        {

        }



        public DbSet<TodoItem> TodoItems { get; set; }   //测试
        public DbSet<Contract_Cargoowner> Contract_Cargoowners{ get; set; }   //货主合同
        public DbSet<Contract_Carriage> Contract_Carriages{ get; set; }//承运合同
        public DbSet<Contract_Currency> Contract_Currencies{ get; set; }//通用合同
        public DbSet<Line> Lines{ get; set; }//路线表
        public DbSet<Shipper> Shippers{ get; set; }//货主表
        public DbSet<Vehicle> Vehicles { get; set; }//车辆表
        public DbSet<Department> Departments { get; set; }//部门表
        public DbSet<Menu> Menus { get; set; }//菜单表
        public DbSet<RoleMenu> RoleMenus { get; set; }//角色菜单表
        public DbSet<Users> User { get; set; }
        public DbSet<BusinessAudit> BusinessAudits { get; set; }  //审核主表
        public DbSet<BusinessAuditLogs> BusinessAuditLogs { get; set; }//审核明细表


        /// <summary>
        /// 这个重写方法，OnModelCreating在数据模型被创建之间运行的
        /// </summary>
        /// <param name="builder"></param>
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            /* Include modules to your migration db context */

            builder.ConfigurePermissionManagement();
            builder.ConfigureSettingManagement();
            builder.ConfigureBackgroundJobs();
            builder.ConfigureAuditLogging();
            builder.ConfigureIdentity();
            builder.ConfigureIdentityServer();
            builder.ConfigureFeatureManagement();
            builder.ConfigureTenantManagement();

            /* Configure your own tables/entities inside here */

            builder.Entity<TodoItem>(b =>
            {
                b.ToTable("TodoItem");      //表名
                b.Property("Text").HasMaxLength(200)         //字段长度
                                  .HasColumnName("ItemText")//数据库字段名称
                                  .HasColumnType("varchar")//数据库字段类型
                                  .IsRequired();          //非空
                //b.ConfigureByConvention(); //auto configure for the base class props
                //...
            });
            builder.Entity<Contract_Cargoowner>(b =>
            {
                b.ToTable("Contract_Cargoowner");//表名
                b.Property("CreateTime").HasColumnType("date").IsRequired();
                b.Property("ContractNum").HasMaxLength(100).IsRequired();
                b.Property("ContractTitle").HasMaxLength(100).IsRequired();
                b.Property("CarriageUnit").HasMaxLength(100).IsRequired();
                b.Property("CarriageMan").HasMaxLength(100).IsRequired();
                b.Property("Line").HasMaxLength(100).IsRequired();
                b.Property("FreightRates").HasPrecision(10, 2).IsRequired();
                b.Property("CharterValue").HasPrecision(10, 2).IsRequired();
                b.Property("SignedTime").HasColumnType("date").IsRequired();
                b.Property("Operator").HasMaxLength(100).IsRequired();
                b.Property("ContractMoney").HasPrecision(10, 2).IsRequired();
                b.Property("ProjectDescription").HasMaxLength(100).IsRequired();
                b.Property("MainClause").HasMaxLength(100).IsRequired();
                b.Property("Accessory").HasMaxLength(100).IsRequired();
                b.Property("Approver").HasMaxLength(100).IsRequired();
            });
            builder.Entity<Contract_Carriage>(b =>
            {
                b.ToTable("Contract_Carriage");//表名
                b.Property("CreateTime").HasColumnType("date").IsRequired();
                b.Property("ContractNum").HasMaxLength(100).IsRequired();
                b.Property("ContractTitle").HasMaxLength(100).IsRequired();
                b.Property("CarriageUnit").HasMaxLength(100).IsRequired();
                b.Property("CarriageMan").HasMaxLength(100).IsRequired();
                b.Property("Line").HasMaxLength(100).IsRequired();
                b.Property("FreightRates").HasPrecision(10, 2).IsRequired();
                b.Property("CharterValue").HasPrecision(10, 2).IsRequired();
                b.Property("SignedTime").HasColumnType("date").IsRequired();
                b.Property("Operator").HasMaxLength(100).IsRequired();
                b.Property("ContractMoney").HasPrecision(10, 2).IsRequired();
                b.Property("ProjectDescription").HasMaxLength(100).IsRequired();
                b.Property("MainClause").HasMaxLength(100).IsRequired();
                b.Property("Accessory").HasMaxLength(100).IsRequired();
                b.Property("Approver").HasMaxLength(100).IsRequired();
            });
            builder.Entity<Contract_Currency>(b =>
            {
                b.ToTable("Contract_Currency");//表名
                b.Property("CreateTime").HasColumnType("date").IsRequired();
                b.Property("ContractNum").HasMaxLength(100).IsRequired();
                b.Property("ContractTitle").HasMaxLength(100).IsRequired();
                b.Property("CarriageUnit").HasMaxLength(100).IsRequired();
                b.Property("CarriageMan").HasMaxLength(100).IsRequired();
                b.Property("Line").HasMaxLength(100).IsRequired();
                b.Property("FreightRates").HasPrecision(10, 2).IsRequired();
                b.Property("CharterValue").HasPrecision(10, 2).IsRequired();
                b.Property("SignedTime").HasColumnType("date").IsRequired();
                b.Property("Operator").HasMaxLength(100).IsRequired();
                b.Property("ContractMoney").HasPrecision(10, 2).IsRequired();
                b.Property("ProjectDescription").HasMaxLength(100).IsRequired();
                b.Property("MainClause").HasMaxLength(100).IsRequired();
                b.Property("Accessory").HasMaxLength(100).IsRequired();
                b.Property("Approver").HasMaxLength(100).IsRequired();
            });
            builder.Entity<Line>(b =>
            {
                b.ToTable("Line");//表名
                b.Property("CreateTime").HasColumnType("date").IsRequired();
                b.Property("LineName").HasMaxLength(100).IsRequired();
                b.Property("OriginPoint").HasMaxLength(100).IsRequired();
                b.Property("OriginAddress").HasMaxLength(100).IsRequired();
                b.Property("EndPoint").HasMaxLength(100).IsRequired();
                b.Property("EndAddress").HasMaxLength(100).IsRequired();
                b.Property("ShipperName").HasMaxLength(100).IsRequired();
                b.Property("MobilePhoto").HasMaxLength(500).IsRequired();
                b.Property("CompanyName").HasMaxLength(100).IsRequired();
                b.Property("Remark").HasMaxLength(100).IsRequired();
                b.Property("CreatorId").HasMaxLength(100).IsRequired();
            });
            builder.Entity<Shipper>(b =>
            {
                b.ToTable("Shipper");//表名
                //b.Property("CreateTime").HasColumnType("date").IsRequired();
                b.Property("ShipperName").HasMaxLength(100).IsRequired();
                b.Property("MobilePhoto").HasMaxLength(500).IsRequired();
                b.Property("CompanyName").HasMaxLength(100).IsRequired();
                b.Property("Address").HasMaxLength(100).IsRequired();
                b.Property("Remark").HasMaxLength(100).IsRequired();
                b.Property("LlicencePhoto").HasMaxLength(100).IsRequired();
            });
            builder.Entity<Vehicle>(b =>
            {
                b.ToTable("Vehicle");//表名
                b.Property("CreateTime").HasColumnType("date").IsRequired();
                b.Property("Brand").HasMaxLength(100).IsRequired();
                b.Property("LicensePlate").HasMaxLength(100).IsRequired();
                b.Property("Driver").HasMaxLength(100).IsRequired();
                b.Property("Company").HasMaxLength(100).IsRequired();
                b.Property("Size").HasMaxLength(100).IsRequired();
                b.Property("Color").HasMaxLength(100).IsRequired();
                b.Property("VehicleNo").HasMaxLength(100).IsRequired();
                b.Property("Maintenance").HasMaxLength(100).IsRequired();
                b.Property("VehiclePhoto").HasMaxLength(500).IsRequired();
                b.Property("MaintenancePhoto").HasMaxLength(500).IsRequired();
                b.Property("CreatorId").HasMaxLength(500).IsRequired();
            });
            builder.Entity<Department>(b => {
                b.ToTable("Department");//表名
                b.Property("CreateTime").HasColumnType("date").IsRequired();
                b.Property("CreatorId").HasMaxLength(100).IsRequired();
                b.Property("Name").HasMaxLength(50).IsRequired();
                b.Property("ParentId").HasMaxLength(100).IsRequired();
            });
            builder.Entity<Menu>(b => {
                b.ToTable("Menu");//表名
                b.Property("ModId").HasMaxLength(100).IsRequired();
                b.Property("ParnetId").HasMaxLength(100).IsRequired();
                b.Property("MenuName").HasMaxLength(50).IsRequired();
                b.Property("Link").HasMaxLength(100).IsRequired();
                b.Property<int>("Status").HasColumnType("int").IsRequired();
                b.Property<int>("Sort").HasColumnType("int").IsRequired();
            });
            builder.Entity<RoleMenu>(b => {
                b.ToTable("RoleMenu");//表名
            });
        }
    }
}
